#include <graphics.h>
#include <conio.h>
#include <string.h>
#pragma comment(lib,"Winmm.lib")
#define high 600
#define width 500
//全局变量
int end=0;
int plane_x,plane_y;
int bullet_x,bullet_y;
int enemy_x,enemy_y;
int i;
char score[2]={0};
IMAGE img_bk;	//定义IMAGE对象背景
IMAGE img_plane1,img_plane2;
IMAGE img_bullet1,img_bullet2;
IMAGE img_enemy1,img_enemy2;
void start()
{
	mciSendString("open E:\\game_music.mp3 alias bkmusic",NULL,0,NULL);//背景音
	mciSendString("play bkmusic repeat",NULL,0,NULL);//循环播放

	plane_x=width/2;
	plane_y=500;
	bullet_x=width;
	bullet_y=high;
	enemy_x=rand()%width;
	enemy_y=0;
	initgraph(width,high);

	loadimage(&img_bk,"E:\\background.jpg");	//读取图片到img对象中
	loadimage(&img_plane1,"E:\\planeNormal_1.jpg");
	loadimage(&img_plane2,"E:\\planeNormal_2.jpg");
	loadimage(&img_bullet1,"E:\\bullet1.jpg");
	loadimage(&img_bullet2,"E:\\bullet2.jpg");
	loadimage(&img_enemy1,"E:\\enemyPlane1.jpg");
	loadimage(&img_enemy2,"E:\\enemyPlane2.jpg");
	BeginBatchDraw();
}

void show()
{
	putimage(0,0,&img_bk);	//在坐标（0,0）位置显示IMAGE对象	
	putimage(enemy_x-50,enemy_y-60,&img_enemy1,NOTSRCERASE);
	putimage(enemy_x-50,enemy_y-60,&img_enemy2,SRCINVERT);

	putimage(plane_x-50,plane_y-60,&img_plane1,NOTSRCERASE);
	putimage(plane_x-50,plane_y-60,&img_plane2,SRCINVERT);

	putimage(bullet_x,bullet_y,&img_bullet1,NOTSRCERASE);//子弹
	putimage(bullet_x,bullet_y,&img_bullet2,SRCINVERT);

	outtextxy(width-50,high+20,"score");//显示文字

	FlushBatchDraw();
	Sleep(50);
}
void updatewithoutInput()
{	
	if(bullet_y>-50)
		bullet_y-=5;
	if(enemy_y<high+50)
	{
		enemy_y+=3;
	}
	else
	{
		enemy_x=rand()%width;//出了画布，重新读取敌机
		enemy_y=-50;
	}
	if(abs(enemy_x-plane_x)+abs(enemy_y-plane_y)<100)//如果敌机与我机相撞
	{
		IMAGE plane_explode1,plane_explode2;//爆炸效果
		loadimage(&plane_explode1,"E:\\planeExplode_1.jpg");
		loadimage(&plane_explode2,"E:\\planeExplode_2.jpg");
		putimage(plane_x-50,plane_y-60,&plane_explode1,NOTSRCERASE);
		putimage(plane_x-50,plane_y-60,&plane_explode2,SRCINVERT);
		//爆炸音效
		mciSendString("close explode",NULL,0,NULL);//先把前一次背景乐关掉
		mciSendString("open E:\\explode.mp3 alias explode",NULL,0,NULL);//打开跳动音乐
		mciSendString("play explode",NULL,0,NULL);//仅播放一次
		end=1;
	}
	if(abs(enemy_x-bullet_x)+abs(enemy_y-bullet_y)<50)//如果敌机被击中
	{
		mciSendString("close gotEnemy",NULL,0,NULL);//先把前一次背景乐关掉
		mciSendString("open E:\\gotEnemy.mp3 alias gotEnemy",NULL,0,NULL);//打开跳动音乐
		mciSendString("play gotEnemy",NULL,0,NULL);//仅播放一次

		enemy_y=-50;//重新读入敌机
		enemy_x=rand()%width;
	}
}
void updatewithInput()
{
	MOUSEMSG m;
	while(MouseHit())
	{
		m=GetMouseMsg();

		if(m.uMsg==WM_MOUSEMOVE)//鼠标移动时飞机的位置
		{
			plane_x=m.x;
			plane_y=m.y;
		}
		else if(m.uMsg==WM_LBUTTONDOWN)//按下鼠标左键发射子弹
		{
			bullet_x=plane_x;
			bullet_y=plane_y-50;

			mciSendString("close f_gun",NULL,0,NULL);//先把前一次背景乐关掉
			mciSendString("open E:\\f_gun.mp3 alias f_gun",NULL,0,NULL);//打开跳动音乐
			mciSendString("play f_gun",NULL,0,NULL);//仅播放一次
		}
	}
}
void gameover()
{
	EndBatchDraw();
	getch();
	closegraph();
}
int main()
{
	start();//初始化
	while(1)//循环执行
	{
		show();//显示画面
		updatewithoutInput();
		updatewithInput(); 
		if(end==1)//游戏结束
			break;
	}
	gameover();//游戏结束
	return 0;
}
